import uuid
import json
import common_func
import logging

from common_func import print_with_location
from typing import Optional, List


class NetworkJSONBuilder:
    def __init__(
        self, trans_id: Optional[str] = None, code: int = 200, msg: str = "success"
    ):
        self.trans_id = trans_id or str(uuid.uuid4())
        self.code = code
        self.msg = msg

    def _base_json(self, method: str) -> dict:
        return {
            "id": str(uuid.uuid4()),
            "transId": self.trans_id,
            "method": method,
            "version": "1.0.0",
            "code": self.code,
            "msg": self.msg,
        }

    def build_4g_status_reply(self, status: int, csq: int = 0, ip: str = "") -> dict:
        data = {"csq": csq, "status": status, "ip": ip}
        base = self._base_json("network.4g.status.getReply")
        base["data"] = data
        return base

    def build_4g_config_push_reply(self) -> dict:
        return self._base_json("network.4g.config.pushReply")

    def build_4g_config_get_reply(self, enable, enableToConnect) -> dict:
        data = {"enable": enable, "enableToConnect": enableToConnect}
        base = self._base_json("network.4g.config.getReply")
        base["data"] = data
        return base

    def build_wifi_config_push_reply(self) -> dict:
        return self._base_json("network.wifi.config.pushReply")

    def build_wifi_config_get_reply(
        self, enable, enableToConnect, ssid, password, security=""
    ) -> dict:
        data = {
            "enable": enable,
            "enableToConnect": enableToConnect,
            "ssid": ssid,
            "password": password,
            "security": security,
        }
        base = self._base_json("network.wifi.config.getReply")
        base["data"] = data
        return base

    def build_wifi_config_status_reply(
        self, ssid, ip, status, interface, rssi: int = 0
    ) -> dict:
        data = {
            "ssid": ssid,
            "ip": ip,
            "rssi": rssi,
            "status": status,
            "interface": interface,
        }
        base = self._base_json("network.wifi.status.getReply")
        base["data"] = data
        return base

    def build_wifi_scan_push_reply(self, wifi_list: List[dict]):
        data = []
        for wifi in wifi_list:
            item = {
                "ssid": wifi.get("ssid", ""),
                "security": wifi.get("security", ""),
                "rssi": wifi.get("rssi", 0),
                "status": wifi.get("status", ""),
            }
            data.append(item)
        base = self._base_json("network.wifi.config.scan.pushReply")
        base["data"] = data
        return base

    def build_network_status_update(
        self,
        status: int,
        hw_type: str,
        ssid: str = "",
        error_code: int = None,
        error_msg: str = None,
    ) -> dict:
        data = {"ssid": ssid, "status": status, "hwType": hw_type}
        if status == 3:
            data["errorCode"] = error_code or 0
            data["errorMsg"] = error_msg or ""
        base = self._base_json("network.status.update")
        base["data"] = data
        return base


# 使用示例
# builder = NetworkJSONBuilder()

# json1 = builder.build_4g_status_reply(rssi=-70, status=2)
# json2 = builder.build_network_status_update(
#     status=3, hw_type="wifi", ssid="MyWiFi", error_code=404, error_msg="Not found"
# )
# json3 = builder.build_4g_config_push_reply()
# json4 = builder.build_wifi_scan_push_reply(
#     [
#         {"ssid": "HomeWiFi", "security": "WPA2", "rssi": -50, "status": "connected"},
#         {
#             "ssid": "GuestWiFi",
#             "security": "none",
#             "rssi": -75,
#             "status": "disconnected",
#         },
#     ]
# )

# # 打印其中一个示例
# print_with_location(json.dumps(json1, indent=2))
